package cc.catface.service;

import cc.catface.common.output.Result;
import cc.catface.dao.biz.app.*;
import cc.catface.dao.biz.app.dto.*;
import cc.catface.dao.biz.app.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
@Service
public class ApBehaviorServiceImpl extends BaseService implements ApBehaviorService {
    @Autowired
    private ApUserMapperServiceImpl apUserMapperService;
    @Autowired
    private ApArticleMapperServiceImpl apArticleMapperService;
    @Autowired
    private ApLikesBehaviorMapperServiceImpl apLikesBehaviorMapperService;
    @Autowired
    private ApCollectionMapperServiceImpl apCollectionMapperService;
    @Autowired
    private ApShareBehaviorMapperServiceImpl apShareBehaviorMapperService;
    @Autowired
    private ApCommentMapperServiceImpl apCommentMapperService;
    @Autowired
    private ApCommentRepayMapperServiceImpl apCommentRepayMapperService;

    @Override
    public Result<ShareOUTDTO> share(ShareINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        ApArticle article = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", in.getArticleId()));
        if (article == null) return Result.error("未查询到文章或动态");
        ApShareBehavior shareBehavior = new ApShareBehavior();
        shareBehavior.setUserId(userId);
        shareBehavior.setArticleId(in.getArticleId());
        shareBehavior.setType(in.getType());
        shareBehavior.setCreatedTime(in.getCreatedTime());
        boolean save = apShareBehaviorMapperService.save(shareBehavior);
        ShareOUTDTO out = new ShareOUTDTO();
        out.setSuccess(save);
        return Result.success(out);
    }

    @Override
    public Result<CollectOUTDTO> collect(CollectINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        ApArticle article = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", in.getArticleId()));
        if (article == null) return Result.error("未查询到文章或动态");
        List<ApCollection> list = apCollectionMapperService.list(new QueryWrapper<ApCollection>().eq("user_id", userId).eq("article_id", in.getArticleId()));
        if (list != null && list.size() > 0) return Result.error("该文章已收藏");

        ApCollection apCollection = new ApCollection();
        apCollection.setUserId(userId);
        apCollection.setArticleId(in.getArticleId());
        apCollection.setType(in.getType());
        apCollection.setCollectionTime(in.getCollectionTime());
        apCollection.setPublishedTime(in.getPublishTime());
        boolean save = apCollectionMapperService.save(apCollection);
        CollectOUTDTO out = new CollectOUTDTO();
        out.setSuccess(save);
        return Result.success(out);
    }

    @Override
    public Result<DisCollectOUTDTO> disCollect(DisCollectINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        ApArticle article = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", in.getArticleId()));
        if (article == null) return Result.error("未查询到文章或动态");
        List<ApCollection> list = apCollectionMapperService.list(new QueryWrapper<ApCollection>().eq("user_id", userId).eq("article_id", in.getArticleId()));
        if (list == null && list.size() < 1) return Result.error("未收藏该文章");
        boolean remove = apCollectionMapperService.remove(new QueryWrapper<ApCollection>().eq("user_id", userId).eq("article_id", in.getArticleId()));
        DisCollectOUTDTO out = new DisCollectOUTDTO();
        out.setSuccess(remove);
        return Result.success(out);
    }

    @Override
    public Result<List<ListCollectOUTDTO>> listCollect(ListCollectINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        List<ApCollection> list = apCollectionMapperService.list(new QueryWrapper<ApCollection>().eq("user_id", userId));
        if (list == null || list.size() < 1) return Result.success(null);
        List<ListCollectOUTDTO> outs = new ArrayList<>();
        for (ApCollection collection : list) {
            ListCollectOUTDTO out = new ListCollectOUTDTO();
            out.setUserId(userId);
            out.setArticleId(collection.getArticleId());
            out.setType(collection.getType());
            out.setCollectionTime(collection.getCollectionTime());
            //文章详情
            ApArticle one = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", collection.getArticleId()));
            if (one == null) continue;
            out.setArticle(one);
            outs.add(out);
        }
        return Result.success(outs);
    }

    @Override
    public Result<CommentOUTDTO> comment(CommentINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        String userName = user.getName();
        ApArticle article = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", in.getArticleId()));
        if (article == null) return Result.error("未查询到文章或动态");
        ApComment apComment = new ApComment();
        apComment.setUserId(userId);
        apComment.setUserName(userName);
        apComment.setArticleId(in.getArticleId());
        apComment.setChannelId(in.getChannelId());
        apComment.setType(in.getType());
        apComment.setContent(in.getContent());
        apComment.setImage(in.getImageUrl());
        apComment.setFlag(in.getFlag());
        apComment.setAddress(in.getAddress());
        apComment.setCreatedTime(in.getCreatedTime());
        apComment.setUpdatedTime(in.getCreatedTime());
        boolean save = apCommentMapperService.save(apComment);
        CommentOUTDTO out = new CommentOUTDTO();
        out.setSuccess(save);
        return Result.success(out);
    }

    @Override
    public Result<DeleteCommentOUTDTO> deleteComment(DeleteCommentINDTO in) {
        boolean remove = apCommentMapperService.remove(new QueryWrapper<ApComment>().eq("id", in.getCommentId()));
        DeleteCommentOUTDTO out = new DeleteCommentOUTDTO();
        out.setSuccess(remove);
        return Result.success(out);
    }

    @Override
    public Result<CommentRepayOUTDTO> commentRepay(CommentRepayINDTO in) {
        ApUser user = apUserMapperService.getOne(new QueryWrapper<ApUser>().eq("phone", in.getPhone()));
        if (user == null) return Result.error("未查询到用户");
        Long userId = user.getId();
        String userName = user.getName();
        ApComment comment = apCommentMapperService.getOne(new QueryWrapper<ApComment>().eq("id", in.getCommentId()));
        if (comment == null) return Result.error("未查询评论ID对应的评论记录");
        ApCommentRepay commentRepay = new ApCommentRepay();
        commentRepay.setUserId(userId);
        commentRepay.setUserName(userName);
        commentRepay.setCommentId(in.getCommentId());
        commentRepay.setContent(in.getContent());
        commentRepay.setAddress(in.getAddress());
        commentRepay.setCreatedTime(in.getCreatedTime());
        boolean save = apCommentRepayMapperService.save(commentRepay);
        CommentRepayOUTDTO out = new CommentRepayOUTDTO();
        out.setSuccess(save);
        return Result.success(out);
    }

    @Override
    public Result<DeleteCommentRepayOUTDTO> deleteCommentRepay(DeleteCommentRepayINDTO in) {
        boolean remove = apCommentRepayMapperService.remove(new QueryWrapper<ApCommentRepay>().eq("id", in.getRepayId()));
        DeleteCommentRepayOUTDTO out = new DeleteCommentRepayOUTDTO();
        out.setSuccess(remove);
        return Result.success(out);
    }

    @Override
    public Result<LikeOUTDTO> like(LikeINDTO in) {
        //判断文章是否存在
        ApArticle article = apArticleMapperService.getOne(new QueryWrapper<ApArticle>().eq("id", in.getArticleId()));
        if (article == null) return Result.error("无该文章");
        long userId = getUserIdByPhone(in.getPhone());
        long likes = article.getLikes();
        //更新ap_likes_behavior表
        ApLikesBehavior one = apLikesBehaviorMapperService.getOne(new QueryWrapper<ApLikesBehavior>().eq("user_id", userId).eq("article_id", in.getArticleId()));
        LikeOUTDTO out = new LikeOUTDTO();
        if (one == null) {
            if (in.getOperation() != 0) return Result.error("新增操作类型不可为非点赞");
            ApLikesBehavior likesBehavior = new ApLikesBehavior();
            likesBehavior.setUserId(userId);
            likesBehavior.setArticleId(in.getArticleId());
            likesBehavior.setType(in.getType());
            likesBehavior.setOperation(in.getOperation());
            likesBehavior.setCreatedTime(in.getCreatedTime());
            out.setSuccess(apLikesBehaviorMapperService.save(likesBehavior));
        } else {
            if (one.getOperation() == in.getOperation()) return Result.error("操作类型不可与表中一致");
            UpdateWrapper<ApLikesBehavior> likesBehaviorUpdateWrapper = new UpdateWrapper<>();
            likesBehaviorUpdateWrapper.eq("user_id", userId);
            likesBehaviorUpdateWrapper.eq("article_id", in.getArticleId());
            likesBehaviorUpdateWrapper.set("operation", in.getOperation());
            likesBehaviorUpdateWrapper.set("created_time", in.getCreatedTime());
            out.setSuccess(apLikesBehaviorMapperService.update(likesBehaviorUpdateWrapper));
        }
        apArticleMapperService.update(new UpdateWrapper<ApArticle>().eq("id", in.getArticleId()).set("likes", (in.getOperation() == 0) ? (++likes) : (--likes)));

        return Result.success(out);
    }
}
